package shuimin.weixin.contacts.departure;

import pond.common.S;
import pond.common.STRING;
import pond.web.EndToEndException;
import shuimin.weixin.Constant;
import shuimin.weixin.HTTP;
import shuimin.weixin.security.AccessToken;

import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * Created by sky on 16/5/19.
 */
public class DepartureManage {

    public Map<String,Object> create(Departure departure){
        String url= Constant.QY_API_URL_PREFIX+Constant.CONTACTS_DEPARTURE_CREATE+"access_token="+ AccessToken.getAccessToken();
        try {
            return HTTP.postJson(url,departure);
        } catch (UnsupportedEncodingException e) {
            throw new EndToEndException(500,e.getMessage());
        }
    }

    /**
     * 更新部门信息
     * @param departure
     * @return
     */
    public Map<String,Object> update(Departure departure){
        String url= Constant.QY_API_URL_PREFIX+Constant.CONTACTS_DEPARTURE_CREATE+"access_token="+ AccessToken.getAccessToken();
        try {
            return HTTP.postJson(url,departure);
        } catch (UnsupportedEncodingException e) {
            throw new EndToEndException(500,e.getMessage());
        }
    }

    /**
     * 删除部门（注：不能删除根部门(id=1)；不能删除含有子部门、成员的部门）
     * @param id
     * @return
     */
    public Map<String,Object> delete(String id){
        S._assert(id);
        String url= Constant.QY_API_URL_PREFIX+Constant.CONTACTS_DEPARTURE_CREATE+"access_token="+ AccessToken.getAccessToken()+"&id="+id;
        try {
            return HTTP.get(url);
        } catch (UnsupportedEncodingException e) {
            throw new EndToEndException(500,e.getMessage());
        }
    }

    /**
     * 获取部门详情,可指定部门,根部门id为1
     * @param id
     * @return
     */
    public Map<String,Object> list(String id){
        String url;
        if(STRING.isBlank(id)){
            url= Constant.QY_API_URL_PREFIX+Constant.CONTACTS_DEPARTURE_CREATE+"access_token="+ AccessToken.getAccessToken();
        }else{
            url= Constant.QY_API_URL_PREFIX+Constant.CONTACTS_DEPARTURE_CREATE+"access_token="+ AccessToken.getAccessToken()+"&id="+id;
        }
        try {
            return HTTP.get(url);
        } catch (UnsupportedEncodingException e) {
            throw new EndToEndException(500,e.getMessage());
        }
    }
}
